﻿using AutoMapper;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using System.Numerics;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using VM.Common.DataUntil;

using VM.Common.Result;
using VM.DAL.System.Code;
using VM.Entity.EntityDto.Menu;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Token;
using VM.IBLL.Menu;
using VM.IDAL;
using VM.IDAL.Menu;
using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.Menu
{
    public class MenuBLL : BaseBLL, IMenuBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private readonly IMenuDAL _IMenuDAL;
        private readonly IMapper _IMapper;
        public MenuBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL, IMenuDAL iMenuDAL, IMapper imapper)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
            _IMenuDAL = iMenuDAL;
            _IMapper = imapper;
        }

        public ApiResult addMenu(addMenuDto obj)
        {
            ApiResult res = new ApiResult();
            sys_menu menu = _IMapper.Map<addMenuDto,sys_menu>(obj);
            //处理表头
            bool addRes = _IMenuDAL.addMenu(menu);
            if (addRes)
            {
                res.code = 201;
                res.msg = "添加成功";
            }
            else
            {
                res.code = 403;
                res.msg = "添加失败";
            }
            return res;
        }

        public ApiDataResult<List<getMenuTreeListDto>> getMenuTreeList(int? UserId, string? menuName)
        {


            List<sys_menu> menuList = _IMenuDAL.getMenuTreeList(menuName);
            //_ISqlSuggarDAL.QueryPageList<sys_menu>(PageSize, PageIndex);


            List<getMenuTreeListDto> TreeRes = _IMapper.Map<List<sys_menu>, List<getMenuTreeListDto>>(menuList);
            ApiDataResult <List<getMenuTreeListDto>> res = new ApiDataResult<List<getMenuTreeListDto>>();
            if (TreeRes.Count > 0)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = TreeRes;
            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;
        }

        public ApiDataResult<menuInfoDetailDto> getMenuInfoById(int? userId, int id)
        {


            menuInfoDetailDto menuInfo = _IMenuDAL.getMenuInfoById(id);
            ApiDataResult<menuInfoDetailDto> res = new ApiDataResult<menuInfoDetailDto>();
            if (menuInfo.id ==id)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = menuInfo;

            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }

            return res;
        }

        public ApiDataResult<PagingData<getMenuListDto>> getMenuList(int pageIndex,int pageSize,int? userId, string? menuName)
        {
            PagingData<getMenuListDto> menuList = _IMenuDAL.getMenuList(pageIndex,pageSize,menuName);
            //_ISqlSuggarDAL.QueryPageList<sys_menu>(PageSize, PageIndex);
             
          ApiDataResult<PagingData<getMenuListDto>> res = new ApiDataResult<PagingData<getMenuListDto>>() {
           code = 200,
           msg = "获取成功",
           data = menuList

        };
            
           
          
            return res;
        }
    }



}
